package com.javastudy.liuhouse.lambda.stream;

import org.junit.Test;

import java.time.DayOfWeek;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class FilterStream {

    @Test
    public void testFilter01(){
        //得到所有数据里面的奇数
        IntStream.of(1,2,3,4,5,6,7,8,9)
                .filter(n -> n % 2 != 0)
                .forEach(System.out::println);
    }


    @Test
    public void testFilter02(){
        Stream.generate(new LocalDateSupplier())
                .limit(31)
                .filter(ldt -> ldt.getDayOfWeek() == DayOfWeek.SATURDAY || ldt.getDayOfWeek() == DayOfWeek.SUNDAY)
                .forEach(System.out::println);
    }


    @Test
    public void testFilter03(){
        //创建一个学生表
        List<Student> students = Arrays.asList(
                new Student("Alice", 85),
                new Student("Bob", 45),
                new Student("Charlie", 60),
                new Student("David", 75),
                new Student("Eva", 50)
        );
        //使用Stream过滤出成绩合格的学生,并打印出名字
        Stream<Student> studentStream = students.stream()
                .filter(student -> student.getScore() >= 60);
        ArrayList<String> student_names = new ArrayList<>();
        studentStream.forEach(student -> {
            student_names.add(student.getName());
        });
        System.out.println(student_names);
    }

}
